package com.fivefaces.ide.plugins.impl;

import com.fivefaces.ide.plugins.CommandBuilder;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

/**
 * User: imran.khan
 * Date: 6/04/11
 * Time: 2:11 PM
 */
public class WindowsCommandBuilder implements CommandBuilder {

    public String [] buildCommand(String filePath) {
        return new String[] {writeBatchFile(filePath)};
    }

    private String writeBatchFile(String path) {
        String result = null;
        FileWriter fileWriter = null;
        try {
            String tempDir = System.getProperty("java.io.tmpdir");
            File executeFile = new File(tempDir + File.separator + new Date().getTime() + ".bat");
            fileWriter = new FileWriter(executeFile);
            fileWriter.write("@echo off\n");
            fileWriter.write("cd \"" + path + "\"\n");
            fileWriter.write("start C:\\Windows\\System32\\cmd.exe");
            result = executeFile.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
